using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace ChuyenDongVatLy
{
   
    public class MatPhangNghieng : Microsoft.Xna.Framework.GameComponent
    {//Chuyen tat ca qua posCenter
        Game1 game;
        BaseObject _vatthe;
        Vector2 S0= new Vector2(30,370);      

        private Texture2D picPlay;
        private Texture2D picStop;
        private Texture2D picPause;
        private Texture2D picControl;
        private Texture2D picRemove;
        private Texture2D picAdd;


        private Texture2D picPlay2;
        private Texture2D picStop2;
        private Texture2D picPause2;
        private Texture2D picControl2;
        private SpriteFont font1;
        private Texture2D picRemove2;
        private Texture2D picAdd2;


        private bool _isPause = true;
        private bool _isMOBPlay = false;
        private bool _isMOBPause = false;
        private bool _isMOBStop = false;
        private bool _isMOBControl = false;
        private bool _isMOBReMove = false;
        private bool _isMOBAdd = false;
        private bool isRemove = false;
       

        MouseGame mouse;        
        Texture2D _thanhchan;      
        float _vantocLast=400;
        float _X;
        float _Y;
        float _f;
        float _heSoMaSat = 0.01f;

       
        float _giaTocBanDau = 100;        
        bool huongdichuyen;
        int numberchange;
        Vector2 _poschange;
        List<ThanhMPN> _listThanh;
        List<float> _listTime;
        List<float> _listTimeBack;
        List<float> _listV0;
        List<float> _listV0back;
        List<ThanhMPN> _listThanhDao;
        List<float> _GiaTocChange;
        List<float> _GiaTocChangeBack;

        ThanhMPN _thanhMPN1;
        int _soLuongMP;

        Geometry ge= new Geometry();
        public MatPhangNghieng(Game1 game1)
            : base(game1)
        {           
            game = game1;
            mouse = new MouseGame(game);
            _listThanh = new List<ThanhMPN>();
            _thanhMPN1 = new ThanhMPN(this.game, 0, 200, new Vector2(0, 400));
            _listThanh.Add(_thanhMPN1);
            //khoi tao thanh mpn
            for (int i = 1; i < 10; i++)
            {
                ThanhMPN thanh = new ThanhMPN(this.game,Math.PI/3,0,Vector2.Zero);
                _listThanh.Add(thanh);
            }
            _listThanhDao = new List<ThanhMPN>();
            for (int i = 0; i < _listThanh.Count; i++)
            {
                _listThanhDao.Add(null);
            }
            for (int i = 0; i < _listThanh.Count; i++)
            {
                _listThanhDao[i] = _listThanh[_listThanh.Count - i-1];
            }
            _vatthe = new BaseObject();
            _vatthe.PosCenter = S0;
            _listTime = new List<float>();
            _listV0 = new List<float>();
            _listTimeBack = new List<float>();
            _listV0back = new List<float>();
            _GiaTocChange = new List<float>();
            _GiaTocChangeBack = new List<float>();
            for (int i = 0; i < 20; i++)
            {
                float x = 0;
                _listTime.Add(x);
                _listV0.Add(x);
                _listTimeBack.Add(x);
                _listV0back.Add(x);
                _GiaTocChange.Add(x);
                _GiaTocChangeBack.Add(x);
            }
            _listV0[0] = _vantocLast;
            _GiaTocChange[0] = _giaTocBanDau;
            _vatthe.V0 = _vantocLast;
            _vatthe.A = _GiaTocChange[0];
            _soLuongMP = 0;//chu y, phai add trc 1 thanh                      

        }
          
        public override void Initialize()
        {
            base.Initialize();
        }
     
        public void LoadContent(ContentManager Content)
        {
            _thanhchan = Content.Load<Texture2D>("thanhgo");
            _vatthe.Picture = Content.Load<Texture2D>("ball");            
            for (int i = 0; i < _listThanh.Count; i++)
            {
                _listThanh[i].LoadContent(Content);
            }
            picControl = Content.Load<Texture2D>("Button\\b_Control");
            picPlay = Content.Load<Texture2D>("Button\\b_Play");
            picPause = Content.Load<Texture2D>("Button\\b_Pause");
            picStop = Content.Load<Texture2D>("Button\\b_Stop");


            picControl2 = Content.Load<Texture2D>("Button\\a_Control");
            picPlay2 = Content.Load<Texture2D>("Button\\a_Play");
            picPause2 = Content.Load<Texture2D>("Button\\a_Pause");
            picStop2 = Content.Load<Texture2D>("Button\\a_Stop");

            picAdd = Content.Load<Texture2D>("Button\\b_Add");
            picAdd2 = Content.Load<Texture2D>("Button\\a_Add");
            picRemove = Content.Load<Texture2D>("Button\\b_Remove");
            picRemove2 = Content.Load<Texture2D>("Button\\a_Remove");


            font1 = Content.Load<SpriteFont>("font");
        }
        public override void Update(GameTime gameTime)
        {

            MouseOverButton();
            CheckButton();
            CreateFormAdd();
            CreateForm();
            ChuyenDong(gameTime);
            base.Update(gameTime);
        }
        public void ChuyenDong(GameTime gameTime)
        {
            if (_isPause==false)
            {
                if (LineChangeV() != Vector2.Zero)
                {
                    _poschange = LineChangeV();
                }
                if (NumberOfLineChange() > 0)
                {
                    numberchange = NumberOfLineChange();
                }
                if (_vatthe.V >= 0)
                    huongdichuyen = true;
                else
                    huongdichuyen = false;
                for (int i = 0; i < _listThanh.Count; i++)
                {
                    if (CheckCollisionMPN(_listThanh[i], _vatthe))
                    {
                        if (huongdichuyen)
                        {
                            for (int j = 0; j < _listThanh.Count; j++)
                            {
                                if (j != i)
                                {
                                    _listTime[j] = 0;
                                    _listV0[j] = 0;
                                    _GiaTocChange[j] = 0;
                                }
                            }
                            float sx;
                            if (i == 0)
                                sx = S0.X;
                            else
                                sx = 0;
                           // _vatthe.A = _GiaTocChange[i] - DeltaAcceleration(_listThanh[i]);
                            _vatthe.A = _giaTocBanDau - DeltaAcceleration(_listThanh[i]);
                            _GiaTocChange[i + 1] = _vatthe.A;
                            _listTime[i] += ((float)gameTime.ElapsedGameTime.Milliseconds / 1000);
                            _X = (float)((_listV0[i] * _listTime[i] + 0.5 * _vatthe.A * _listTime[i] * _listTime[i]) * Math.Cos(_listThanh[i].Angle)) + _listThanh[i].Position.X + sx;
                            _Y = -(float)((_listV0[i] * _listTime[i] + 0.5 * _vatthe.A * _listTime[i] * _listTime[i]) * Math.Sin(_listThanh[i].Angle)) + _listThanh[i].Position.Y - ConstPhysic._R;
                            _vatthe.V = _listV0[i] + _vatthe.A * _listTime[i];
                            _listV0[i + 1] = _vatthe.V;
                        }
                        else
                        {
                            for (int j = 0; j < _listThanhDao.Count; j++)
                            {
                                if (j != _listThanhDao.Count - i - 1)
                                {
                                    _listTimeBack[j] = 0;
                                    _listV0back[j] = 0;
                                    _GiaTocChangeBack[j] = 0;
                                }
                            }
                            int k = _listThanhDao.Count - i - 1;
                            if (i == numberchange)
                            {
                                _listTimeBack[k] += ((float)gameTime.ElapsedGameTime.Milliseconds / 1000);
                                _X = (float)((_listV0back[k] * _listTimeBack[k] + 0.5 * _vatthe.A * _listTimeBack[k] * _listTimeBack[k]) * Math.Cos(_listThanhDao[k].Angle)) + _poschange.X;
                                _Y = -(float)((_listV0back[k] * _listTimeBack[k] + 0.5 * _vatthe.A * _listTimeBack[k] * _listTimeBack[k]) * Math.Sin(_listThanhDao[k].Angle)) + _poschange.Y;
                                _vatthe.V = _listV0back[k] + _vatthe.A * _listTimeBack[k];
                                _listV0back[k + 1] = _vatthe.V;
                                _GiaTocChangeBack[k + 1] = _vatthe.A;
                            }
                            else
                            {
                                //_vatthe.A = _GiaTocChangeBack[k] - DeltaAcceleration(_listThanhDao[k]);
                                _vatthe.A = _giaTocBanDau - DeltaAcceleration(_listThanhDao[i]);
                                _listTimeBack[k] += ((float)gameTime.ElapsedGameTime.Milliseconds / 1000);
                                _GiaTocChangeBack[k + 1] = _vatthe.A;
                                _X = (float)((_listV0back[k] * _listTimeBack[k] + 0.5 * _vatthe.A * _listTimeBack[k] * _listTimeBack[k]) * Math.Cos(_listThanhDao[k].Angle)) + _listThanhDao[k].PointEnd().X;
                                _Y = -(float)((_listV0back[k] * _listTimeBack[k] + 0.5 * _vatthe.A * _listTimeBack[k] * _listTimeBack[k]) * Math.Sin(_listThanhDao[k].Angle)) + _listThanhDao[k].PointEnd().Y - ConstPhysic._R;
                                _listV0back[k + 1] = _vatthe.V;
                            }
                        }
                    }
                }
                _vatthe.PosCenter = new Vector2(_X, _Y);   
            }
                
        }
        public bool CheckCollisionMPN(ThanhMPN thanh,BaseObject vatthe)
        {
            Geometry ge1 = new Geometry();
            if (ge1.DistancePointAndLine(vatthe.PosCenter, thanh.LineOfMPN()) <= ConstPhysic._R+10 &&thanh.Position.X<=vatthe.PosCenter.X&&vatthe.PosCenter.X<=thanh.PointEnd().X)
            {
                return true;
            }
            return false;
        }
        public Vector2 LineChangeV()
        {
            for (int i = 0; i < _listThanh.Count; i++)
            {
                if (CheckCollisionMPN(_listThanh[i],_vatthe))
                {
                    if (_vatthe.V>=-10&&_vatthe.V<0)
                    {
                        return _vatthe.PosCenter;
                    }                    
                }
            }
            return Vector2.Zero;
        }
        public int NumberOfLineChange()
        {
            for (int i = 0; i < _listThanh.Count; i++)
            {
                if (CheckCollisionMPN(_listThanh[i],_vatthe))
                {
                    if (_vatthe.V<0&&_vatthe.V>=-10)
                    {
                        return i;
                    }
                }
            }
            return -1;
        }
        public void Draw(SpriteBatch sp)
        {
            DrawButton(sp);
            Rectangle soure= new Rectangle(0,0,400,4);
            Rectangle soure2 = new Rectangle(0, 0, 40, 15);
            for (int i = 0; i < _listThanh.Count; i++)
            {
                _listThanh[i].Draw(sp);
            }
                sp.Draw(_vatthe.Picture, new Rectangle((int)(_vatthe.PosCenter.X-ConstPhysic._R),(int)(_vatthe.PosCenter.Y-ConstPhysic._R),60,60), Color.White);               
        }
        public void DrawButton(SpriteBatch sp)
        {
            if (_isMOBPlay)
                sp.Draw(picPlay2, ConstPhysic.btnPlay2, Color.White);
            else
                sp.Draw(picPlay, ConstPhysic.btnPlay, Color.White);
            if (_isMOBStop)
                sp.Draw(picStop2, ConstPhysic.btnStop2, Color.White);
            else
                sp.Draw(picStop, ConstPhysic.btnStop, Color.White);
            if (_isMOBPause)
                sp.Draw(picPause2, ConstPhysic.btnPause2, Color.White);
            else
                sp.Draw(picPause, ConstPhysic.btnPause, Color.White);
            if (_isMOBControl)
                sp.Draw(picControl2, ConstPhysic.btnControl2, Color.White);
            else
                sp.Draw(picControl, ConstPhysic.btnControl, Color.White);
            if (_isMOBAdd)
                sp.Draw(picAdd2,ConstPhysic.btnAdd,Color.White);
            else
                sp.Draw(picAdd, ConstPhysic.btnAdd, Color.White);
            if (_isMOBReMove)
                sp.Draw(picRemove, ConstPhysic.btnRemove, Color.White);
            else
                sp.Draw(picRemove2, ConstPhysic.btnRemove, Color.White);

        }
        public float DeltaAcceleration(ThanhMPN mpn)
        {
            float a;
            a = (float)(_vatthe.M * ConstPhysic.Gravity * Math.Sin(mpn.Angle)*100);
            return a;
        }
        
        public void AddMPN(string gocnghieng,string chieudai,bool CreateForm)
        {
            if (_soLuongMP < 10)
            {
                double gocnghieng1 = double.Parse(gocnghieng)*Math.PI;
                float chieudai1 = float.Parse(chieudai);
                _listThanh[_soLuongMP + 1].Angle = gocnghieng1;
                _listThanh[_soLuongMP + 1].Length = chieudai1;
                _listThanh[_soLuongMP + 1].Position = _listThanh[_soLuongMP].PointEnd();
                _soLuongMP++;
            }
            TimeGame._ShowForm = CreateForm;
        }
        public void SetValueShowForm(bool createForm)
        {
            TimeGame._ShowForm = createForm;
        }
        public void CreateFormAdd()
        {
            if (_soLuongMP<10)
            {
                if (mouse.CheckMouse(ConstPhysic.btnAdd) && TimeGame._ShowForm && Mouse.GetState().LeftButton == ButtonState.Pressed)
                {
                    frMPN fr = new frMPN();
                    fr.GetMyValue = new frMPN.GetString(AddMPN);
                    fr.GetMyValue2 = new frMPN.GetString2(SetValueShowForm);
                    fr.Show();
                    TimeGame._ShowForm = false;
                }      
            }                                                              
        }
        public void MouseOverButton()
        {
            if (mouse.CheckMouse(ConstPhysic.btnControl)) _isMOBControl = true;
            else _isMOBControl = false;
            if (mouse.CheckMouse(ConstPhysic.btnPause)) _isMOBPause = true;
            else _isMOBPause = false;
            if (mouse.CheckMouse(ConstPhysic.btnPlay)) _isMOBPlay = true;
            else _isMOBPlay = false;
            if (mouse.CheckMouse(ConstPhysic.btnStop)) _isMOBStop = true;
            else _isMOBStop = false;
            if (mouse.CheckMouse(ConstPhysic.btnAdd)) _isMOBAdd = true;
            else _isMOBAdd = false;
            if (mouse.CheckMouse(ConstPhysic.btnRemove)) _isMOBReMove = true;
            else _isMOBReMove = false;

        }
        public void CheckButton()
        {
            if (mouse.CheckMouse(ConstPhysic.btnPlay) && Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                _isPause = false;
                //ReSet();
            }
            if (mouse.CheckMouse(ConstPhysic.btnPause) && Mouse.GetState().LeftButton == ButtonState.Pressed)
                _isPause = true;
            if (mouse.CheckMouse(ConstPhysic.btnStop) && Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                _isPause = true;
                ReSet();
            }
            if (mouse.CheckMouse(ConstPhysic.btnRemove) && Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                isRemove = true;
            }
            if (isRemove)
            {
                RemoveMPN();
            }
        }
        public void RemoveMPN()
        {
            if (isRemove)
            {
                for (int i = 1 ; i < 10; i++)
                {
                    _listThanh[i].Angle = 0;
                    _listThanh[i].Length = 0;
                    _listThanh[i].Position = Vector2.Zero;
                }
                isRemove = false;
                _soLuongMP = 0;
                _vatthe.PosCenter = S0;
            }
        }

        private void ReSet()
        {
            _vatthe.PosCenter = S0;
            for (int i = 0; i < 20; i++)
            {
                _listTime[i] = 0;
                _listV0[i] = 0;
                _listTimeBack[i] = 0;
                _listV0back[i] = 0;
                _GiaTocChange[i] = 0;
                _GiaTocChangeBack[i] = 0;
            }
            _GiaTocChange[0] = _giaTocBanDau;
            _listV0[0] = _vantocLast;
            _vatthe.V0 = _vantocLast;
            _vatthe.A = _GiaTocChange[0];
            _vatthe.V = 0;     
        }
        public void SetValue2(bool CreateForm)
        {
            TimeGame._ShowForm = CreateForm;
        }
        public void SetValue(string vantoc, string luc, string khoiluong, string hesomasat)
        {
            this._f = float.Parse(luc);
            this._heSoMaSat = float.Parse(hesomasat);
            float vantoc1 = float.Parse(vantoc);
            float luc1 = float.Parse(luc);
            float khoiluong1 = float.Parse(khoiluong);
            float hesomasat1 = float.Parse(hesomasat);
            float lucmasat = khoiluong1 * ConstPhysic.Gravity * hesomasat1;
            float luctonghop = luc1 - lucmasat;
            if (luctonghop < 0)
            {
                luctonghop = 0;
            }
            float giatoc1 = luctonghop / khoiluong1;
            _vatthe.M = khoiluong1;

            _vatthe.PosCenter = S0;
            for (int i = 0; i < 10; i++)
            {
                _listTime[i] = 0;
                _listV0[i] = 0;
                _listTimeBack[i] = 0;
                _listV0back[i] = 0;
                _GiaTocChange[i] = 0;
                _GiaTocChangeBack[i] = 0;
            }
            _GiaTocChange[0] = giatoc1;
            _listV0[0] = vantoc1;
            _vatthe.V0 = vantoc1;
            _vatthe.A = _GiaTocChange[0];
            _vatthe.V = 0;
        }
        public void CreateForm()
        {
            if (mouse.CheckMouse(ConstPhysic.btnControl) && TimeGame._ShowForm && Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                FrMPNDefault fr = new FrMPNDefault();
                fr.GetMyValue = new FrMPNDefault.GetString(SetValue);
                fr.GetMyValue2 = new FrMPNDefault.GetString2(SetValue2);
                fr.Show();
                TimeGame._ShowForm = false;
            }
        }
    }
}

